﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace CMSLibrary.TagsCreate.CustomFunction.Inside
{

    /// <summary>
    /// 所在位置
    /// 1. Location(样式名称,结束位置,链接页面)
    /// 2. Location(样式名称,结束位置,链接页面,间隔符)
    /// 3. Location(样式名称,结束位置,链接页面,间隔符,起始位置)
    /// </summary>
    public class Custom_Location : PInside
    {
        #region 参数成员
        /// <summary>
        /// 样式名称
        /// </summary>
        protected string ClassName = string.Empty;
        /// <summary>
        /// 结束位置
        /// </summary>
        protected string EndPath = string.Empty;
        /// <summary>
        /// 起始位置
        /// </summary>
        protected string RootChar = string.Empty;        
        /// <summary>
        /// 间隔符
        /// </summary>
        protected string SpaceChar = "&gt;&nbsp;";
        /// <summary>
        /// 链接页面
        /// </summary>
        protected string LinkPage = "";
        #endregion

        #region 重写的方法
        protected override void SplitFormatParas(string sourceParas)
        {
            string[] paras = GetParasArray(sourceParas);
            //确定重载方法,由多到少
            /********** 3 **********/
            try
            {
                ClassName = paras[0];
                EndPath = paras[1];
                LinkPage = paras[2];
                SpaceChar = paras[3];
                RootChar = paras[4];
                FunctionBurden = BurdenIndex.three;
                return;
            }
            catch { }
            /********** 2 **********/
            try
            {
                ClassName = paras[0];
                EndPath = paras[1];
                LinkPage = paras[2];
                SpaceChar = paras[3];
                FunctionBurden = BurdenIndex.two;
                return;
            }
            catch { }
            /********** 1 **********/
            try
            {
                ClassName = paras[0];
                EndPath = paras[1];
                LinkPage = paras[2];
                FunctionBurden = BurdenIndex.one;
                return;
            }
            catch { throw new Exception("参数不对"); }

        }

        public override string ToString()
        {
            //初始化            
            if(RootChar == string.Empty) { RootChar = "00"; }

            //获取导航
            return NavigationFormat(RootChar, EndPath);
        }

        /// <summary>
        /// 返回导航字符串
        /// </summary>        
        /// <param name="_rootChar">起始路径</param>
        /// <param name="_endChar">结束路径</param>
        /// <returns>首页 > 新闻中心</returns>
        public string NavigationFormat(string _rootPath, string _endPath)
        {
            StringBuilder result = new StringBuilder();
            
            result.Append("<table class='").Append(ClassName).Append("'><tr>");
            //获取节点集合
            DataEntity.tab_Class obj = new DataEntity.tab_Class();
            DataRow[] drs = SelectListForLocation(_rootPath, _endPath);
            if (drs != null && drs.Length != 0)
            {
                string linkurl = string.Empty;
                foreach (DataRow dr in drs)
                {
                    obj.SetValues(dr);
                    linkurl = (LinkPage == string.Empty) ? string.Empty : "href='" + LinkPage + "?" + UIMethod.UI_parameters + "=" +
                    obj.cls_StructId + obj.cls_NodeValue + "'";
                    result.Append("\r\n\t<td>").Append(SpaceChar)
                        .Append("<a ").Append(linkurl).Append(">").Append(obj.cls_NodeText).Append("</a></td>");
                }
            }
            result.Append("\r\n</tr></table>");

            return result.ToString();
        }

        /// <summary>
        /// 选择指定的栏目节点集合
        /// </summary>        
        public DataRow[] SelectListForLocation(string _rootPath, string _endPath)
        {
            //获取层级节点集合
            List<string> structPaths = new List<string>();
            structPaths.Add(_endPath);
            int sLength = _rootPath.Length; //起始路径长度
            string subpath = _endPath;   //每次循环截取的字符串

            int clen = 0;
            while (subpath.Length > sLength)
            {
                //分析截取长度
                clen = subpath.Length - 2;
                subpath = subpath.Substring(0, clen);
                structPaths.Add(subpath);
            }

            if (structPaths.Count == 0) { return null; }

            DataTable dt = GlocalCache.GetProjectNodes_Data();
            List<DataRow> output = new List<DataRow>();
            for (int i = structPaths.Count - 1; i >= 0; i--)
            {
                string tmp = structPaths[i];
                DataRow[] drs = dt.Select("(cls_StructId + cls_NodeValue) = '" + tmp + "'");
                if (drs != null && drs.Length != 0)
                {
                    output.Add(drs[0]);
                }
            }

            return output.ToArray();
        }
        #endregion
    }
}